{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5.2\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "import paddle.nn.functional as F\n",
    "from paddle.nn import Linear\n",
    "import numpy as np\n",
    "import os\n",
    "import json \n",
    "import random\n",
    "\n",
    "print(paddle.__version__)\n",
    "from paddle.nn import Conv2D,MaxPool2D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(mode='train'):\n",
    "    with open('mnist.json') as f:\n",
    "        data = json.load(f)\n",
    "    train_set, val_set, eval_set = data\n",
    "    if mode == 'train':\n",
    "        imgs, labels = train_set[0], train_set[1]\n",
    "    elif mode == 'valid':\n",
    "        imgs, labels = val_set[0], val_set[1]\n",
    "    elif mode == 'eval':\n",
    "        imgs, labels = eval_set[0], eval_set[1]\n",
    "    else:\n",
    "        raise Exception(\"mode can only be one of['train', 'valid', 'eval']\")\n",
    "    print('训练数据集数量:', len(imgs))\n",
    "    imgs_length = len(imgs)\n",
    "    index_list = list(range(imgs_length))\n",
    "    BATCHSIZE = 100\n",
    "\n",
    "    def data_generator():\n",
    "        if mode == 'train':\n",
    "            random.shuffle(index_list)\n",
    "        imgs_list = []\n",
    "        labels_list = []\n",
    "        for i in index_list:\n",
    "            # img=np.array(imgs[i]).astype('float32')\n",
    "            img = np.reshape(imgs[i], [1, 28, 28]).astype('float32')\n",
    "            label = np.reshape(labels[i], [1]).astype('int64')\n",
    "            imgs_list.append(img)\n",
    "            labels_list.append(label)\n",
    "            if len(imgs_list) == BATCHSIZE:\n",
    "                yield np.array(imgs_list), np.array(labels_list)\n",
    "                imgs_list = []\n",
    "                labels_list = []\n",
    "        if len(imgs_list) > 0:\n",
    "            yield np.array(imgs_list), np.array(labels_list)\n",
    "\n",
    "    return data_generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LeNetModel(paddle.nn.Layer):\n",
    "    def __init__(self):\n",
    "        super(LeNetModel, self).__init__()\n",
    "        self.conv1 = paddle.nn.Conv2D(in_channels=1, out_channels=6, kernel_size=5, stride=1)\n",
    "        self.pool1 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)\n",
    "        self.conv2 = paddle.nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1)\n",
    "        self.pool2 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)\n",
    "        self.fc1 = paddle.nn.Linear(256, 120)\n",
    "        self.fc2 = paddle.nn.Linear(120, 84)\n",
    "        self.fc3 = paddle.nn.Linear(84, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.conv1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.pool1(x)\n",
    "        x = self.conv2(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.pool2(x)\n",
    "        x = paddle.flatten(x, start_axis=1, stop_axis=-1)\n",
    "        x = self.fc1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.fc2(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.fc3(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LeNetModel(paddle.nn.Layer):\n",
    "    def __init__(self):\n",
    "        super(LeNetModel, self).__init__()\n",
    "        self.conv1 = paddle.nn.Conv2D(in_channels=1, out_channels=6, kernel_size=5, stride=1)\n",
    "        self.pool1 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)\n",
    "        self.conv2 = paddle.nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1)\n",
    "        self.pool2 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)\n",
    "        self.fc1 = paddle.nn.Linear(256, 120)\n",
    "        self.fc2 = paddle.nn.Linear(120, 84)\n",
    "        self.fc3 = paddle.nn.Linear(84, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.conv1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.pool1(x)\n",
    "        x = self.conv2(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.pool2(x)\n",
    "        x = paddle.flatten(x, start_axis=1, stop_axis=-1)\n",
    "        x = self.fc1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.fc2(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.fc3(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练数据集数量: 50000\n"
     ]
    }
   ],
   "source": [
    "train_loader = load_data('train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练数据集数量: 10000\n"
     ]
    }
   ],
   "source": [
    "valid_loader = load_data('valid')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch:0,batch:0,loss is:[3.178516]\n",
      "epoch:0,batch:200,loss is:[0.6504636]\n",
      "epoch:0,batch:400,loss is:[0.24490003]\n",
      "[validation]accuracy/loss:0.9399999976158142/0.9399999976158142\n",
      "[validation]accuracy/loss:0.9449999928474426/0.9449999928474426\n",
      "[validation]accuracy/loss:0.9366666674613953/0.9366666674613953\n",
      "[validation]accuracy/loss:0.8999999761581421/0.8999999761581421\n",
      "[validation]accuracy/loss:0.8939999341964722/0.8939999341964722\n",
      "[validation]accuracy/loss:0.8933332562446594/0.8933332562446594\n",
      "[validation]accuracy/loss:0.8971428275108337/0.8971428275108337\n",
      "[validation]accuracy/loss:0.8962500095367432/0.8962500095367432\n",
      "[validation]accuracy/loss:0.8911111354827881/0.8911111354827881\n",
      "[validation]accuracy/loss:0.8960000276565552/0.8960000276565552\n",
      "[validation]accuracy/loss:0.8990909457206726/0.8990909457206726\n",
      "[validation]accuracy/loss:0.9016667008399963/0.9016667008399963\n",
      "[validation]accuracy/loss:0.9015384912490845/0.9015384912490845\n",
      "[validation]accuracy/loss:0.9028571844100952/0.9028571844100952\n",
      "[validation]accuracy/loss:0.906000018119812/0.906000018119812\n",
      "[validation]accuracy/loss:0.9093749523162842/0.9093749523162842\n",
      "[validation]accuracy/loss:0.9117646217346191/0.9117646217346191\n",
      "[validation]accuracy/loss:0.9133332371711731/0.9133332371711731\n",
      "[validation]accuracy/loss:0.9152631163597107/0.9152631163597107\n",
      "[validation]accuracy/loss:0.9139999151229858/0.9139999151229858\n",
      "[validation]accuracy/loss:0.9114285707473755/0.9114285707473755\n",
      "[validation]accuracy/loss:0.9095454812049866/0.9095454812049866\n",
      "[validation]accuracy/loss:0.9091304540634155/0.9091304540634155\n",
      "[validation]accuracy/loss:0.909583330154419/0.909583330154419\n",
      "[validation]accuracy/loss:0.910800039768219/0.910800039768219\n",
      "[validation]accuracy/loss:0.9130769371986389/0.9130769371986389\n",
      "[validation]accuracy/loss:0.9137036800384521/0.9137036800384521\n",
      "[validation]accuracy/loss:0.9121428728103638/0.9121428728103638\n",
      "[validation]accuracy/loss:0.911379337310791/0.911379337310791\n",
      "[validation]accuracy/loss:0.909000039100647/0.909000039100647\n",
      "[validation]accuracy/loss:0.9080644845962524/0.9080644845962524\n",
      "[validation]accuracy/loss:0.908750057220459/0.908750057220459\n",
      "[validation]accuracy/loss:0.9100000858306885/0.9100000858306885\n",
      "[validation]accuracy/loss:0.9114706516265869/0.9114706516265869\n",
      "[validation]accuracy/loss:0.9122858047485352/0.9122858047485352\n",
      "[validation]accuracy/loss:0.9125000834465027/0.9125000834465027\n",
      "[validation]accuracy/loss:0.912702739238739/0.912702739238739\n",
      "[validation]accuracy/loss:0.9128947257995605/0.9128947257995605\n",
      "[validation]accuracy/loss:0.912051260471344/0.912051260471344\n",
      "[validation]accuracy/loss:0.9122499227523804/0.9122499227523804\n",
      "[validation]accuracy/loss:0.912195086479187/0.912195086479187\n",
      "[validation]accuracy/loss:0.9123808741569519/0.9123808741569519\n",
      "[validation]accuracy/loss:0.9139533638954163/0.9139533638954163\n",
      "[validation]accuracy/loss:0.9136362671852112/0.9136362671852112\n",
      "[validation]accuracy/loss:0.9142221212387085/0.9142221212387085\n",
      "[validation]accuracy/loss:0.9136955142021179/0.9136955142021179\n",
      "[validation]accuracy/loss:0.914467990398407/0.914467990398407\n",
      "[validation]accuracy/loss:0.9149999618530273/0.9149999618530273\n",
      "[validation]accuracy/loss:0.9142857193946838/0.9142857193946838\n",
      "[validation]accuracy/loss:0.9133999347686768/0.9133999347686768\n",
      "[validation]accuracy/loss:0.9135293364524841/0.9135293364524841\n",
      "[validation]accuracy/loss:0.9144229888916016/0.9144229888916016\n",
      "[validation]accuracy/loss:0.9149055480957031/0.9149055480957031\n",
      "[validation]accuracy/loss:0.915555477142334/0.915555477142334\n",
      "[validation]accuracy/loss:0.9158180952072144/0.9158180952072144\n",
      "[validation]accuracy/loss:0.9166070818901062/0.9166070818901062\n",
      "[validation]accuracy/loss:0.9173683524131775/0.9173683524131775\n",
      "[validation]accuracy/loss:0.9175861477851868/0.9175861477851868\n",
      "[validation]accuracy/loss:0.9177965521812439/0.9177965521812439\n",
      "[validation]accuracy/loss:0.9183332920074463/0.9183332920074463\n",
      "[validation]accuracy/loss:0.9190163612365723/0.9190163612365723\n",
      "[validation]accuracy/loss:0.9199999570846558/0.9199999570846558\n",
      "[validation]accuracy/loss:0.9198412299156189/0.9198412299156189\n",
      "[validation]accuracy/loss:0.9203124642372131/0.9203124642372131\n",
      "[validation]accuracy/loss:0.9193845987319946/0.9193845987319946\n",
      "[validation]accuracy/loss:0.9198484420776367/0.9198484420776367\n",
      "[validation]accuracy/loss:0.9195522665977478/0.9195522665977478\n",
      "[validation]accuracy/loss:0.9198529124259949/0.9198529124259949\n",
      "[validation]accuracy/loss:0.9197101593017578/0.9197101593017578\n",
      "[validation]accuracy/loss:0.9200000166893005/0.9200000166893005\n",
      "[validation]accuracy/loss:0.9204224944114685/0.9204224944114685\n",
      "[validation]accuracy/loss:0.921110987663269/0.921110987663269\n",
      "[validation]accuracy/loss:0.9210957288742065/0.9210957288742065\n",
      "[validation]accuracy/loss:0.9205403923988342/0.9205403923988342\n",
      "[validation]accuracy/loss:0.9207998514175415/0.9207998514175415\n",
      "[validation]accuracy/loss:0.9202629923820496/0.9202629923820496\n",
      "[validation]accuracy/loss:0.9201297760009766/0.9201297760009766\n",
      "[validation]accuracy/loss:0.9198716878890991/0.9198716878890991\n",
      "[validation]accuracy/loss:0.9202530384063721/0.9202530384063721\n",
      "[validation]accuracy/loss:0.9204999208450317/0.9204999208450317\n",
      "[validation]accuracy/loss:0.9208640456199646/0.9208640456199646\n",
      "[validation]accuracy/loss:0.9213413000106812/0.9213413000106812\n",
      "[validation]accuracy/loss:0.922168493270874/0.922168493270874\n",
      "[validation]accuracy/loss:0.9227379560470581/0.9227379560470581\n",
      "[validation]accuracy/loss:0.9230586290359497/0.9230586290359497\n",
      "[validation]accuracy/loss:0.9231393337249756/0.9231393337249756\n",
      "[validation]accuracy/loss:0.9236779808998108/0.9236779808998108\n",
      "[validation]accuracy/loss:0.9242044687271118/0.9242044687271118\n",
      "[validation]accuracy/loss:0.9244942665100098/0.9244942665100098\n",
      "[validation]accuracy/loss:0.9252220988273621/0.9252220988273621\n",
      "[validation]accuracy/loss:0.9258241057395935/0.9258241057395935\n",
      "[validation]accuracy/loss:0.9264129996299744/0.9264129996299744\n",
      "[validation]accuracy/loss:0.9265590906143188/0.9265590906143188\n",
      "[validation]accuracy/loss:0.925851047039032/0.925851047039032\n",
      "[validation]accuracy/loss:0.9256842136383057/0.9256842136383057\n",
      "[validation]accuracy/loss:0.9262499809265137/0.9262499809265137\n",
      "[validation]accuracy/loss:0.9263917803764343/0.9263917803764343\n",
      "[validation]accuracy/loss:0.9259183406829834/0.9259183406829834\n",
      "[validation]accuracy/loss:0.9266666173934937/0.9266666173934937\n",
      "[validation]accuracy/loss:0.926800012588501/0.926800012588501\n",
      "epoch:1,batch:0,loss is:[0.31907746]\n",
      "epoch:1,batch:200,loss is:[0.24165267]\n",
      "epoch:1,batch:400,loss is:[0.22074814]\n",
      "[validation]accuracy/loss:0.949999988079071/0.949999988079071\n",
      "[validation]accuracy/loss:0.9600000381469727/0.9600000381469727\n",
      "[validation]accuracy/loss:0.9533333778381348/0.9533333778381348\n",
      "[validation]accuracy/loss:0.940000057220459/0.940000057220459\n",
      "[validation]accuracy/loss:0.9340000152587891/0.9340000152587891\n",
      "[validation]accuracy/loss:0.9350000023841858/0.9350000023841858\n",
      "[validation]accuracy/loss:0.9414286017417908/0.9414286017417908\n",
      "[validation]accuracy/loss:0.9362499713897705/0.9362499713897705\n",
      "[validation]accuracy/loss:0.9322221279144287/0.9322221279144287\n",
      "[validation]accuracy/loss:0.9389999508857727/0.9389999508857727\n",
      "[validation]accuracy/loss:0.9436362981796265/0.9436362981796265\n",
      "[validation]accuracy/loss:0.9449999332427979/0.9449999332427979\n",
      "[validation]accuracy/loss:0.9446153044700623/0.9446153044700623\n",
      "[validation]accuracy/loss:0.9457141757011414/0.9457141757011414\n",
      "[validation]accuracy/loss:0.9459999203681946/0.9459999203681946\n",
      "[validation]accuracy/loss:0.9487500190734863/0.9487500190734863\n",
      "[validation]accuracy/loss:0.9482353329658508/0.9482353329658508\n",
      "[validation]accuracy/loss:0.9483334422111511/0.9483334422111511\n",
      "[validation]accuracy/loss:0.9500000476837158/0.9500000476837158\n",
      "[validation]accuracy/loss:0.9490000605583191/0.9490000605583191\n",
      "[validation]accuracy/loss:0.948571503162384/0.948571503162384\n",
      "[validation]accuracy/loss:0.9472728371620178/0.9472728371620178\n",
      "[validation]accuracy/loss:0.9460870623588562/0.9460870623588562\n",
      "[validation]accuracy/loss:0.9458333849906921/0.9458333849906921\n",
      "[validation]accuracy/loss:0.9467999935150146/0.9467999935150146\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.948846161365509/0.948846161365509\n",
      "[validation]accuracy/loss:0.9485185146331787/0.9485185146331787\n",
      "[validation]accuracy/loss:0.9464285969734192/0.9464285969734192\n",
      "[validation]accuracy/loss:0.9448275566101074/0.9448275566101074\n",
      "[validation]accuracy/loss:0.9409999847412109/0.9409999847412109\n",
      "[validation]accuracy/loss:0.9396774172782898/0.9396774172782898\n",
      "[validation]accuracy/loss:0.940625011920929/0.940625011920929\n",
      "[validation]accuracy/loss:0.9418181777000427/0.9418181777000427\n",
      "[validation]accuracy/loss:0.9432352781295776/0.9432352781295776\n",
      "[validation]accuracy/loss:0.9440000057220459/0.9440000057220459\n",
      "[validation]accuracy/loss:0.9433333277702332/0.9433333277702332\n",
      "[validation]accuracy/loss:0.9432432055473328/0.9432432055473328\n",
      "[validation]accuracy/loss:0.9434210062026978/0.9434210062026978\n",
      "[validation]accuracy/loss:0.942564070224762/0.942564070224762\n",
      "[validation]accuracy/loss:0.9430000185966492/0.9430000185966492\n",
      "[validation]accuracy/loss:0.942926824092865/0.942926824092865\n",
      "[validation]accuracy/loss:0.9430952668190002/0.9430952668190002\n",
      "[validation]accuracy/loss:0.9437209963798523/0.9437209963798523\n",
      "[validation]accuracy/loss:0.9443182349205017/0.9443182349205017\n",
      "[validation]accuracy/loss:0.9453334212303162/0.9453334212303162\n",
      "[validation]accuracy/loss:0.9447826743125916/0.9447826743125916\n",
      "[validation]accuracy/loss:0.9453192353248596/0.9453192353248596\n",
      "[validation]accuracy/loss:0.9454166293144226/0.9454166293144226\n",
      "[validation]accuracy/loss:0.9453060626983643/0.9453060626983643\n",
      "[validation]accuracy/loss:0.9441999197006226/0.9441999197006226\n",
      "[validation]accuracy/loss:0.944117546081543/0.944117546081543\n",
      "[validation]accuracy/loss:0.9446153044700623/0.9446153044700623\n",
      "[validation]accuracy/loss:0.9445281624794006/0.9445281624794006\n",
      "[validation]accuracy/loss:0.9448146820068359/0.9448146820068359\n",
      "[validation]accuracy/loss:0.9443634748458862/0.9443634748458862\n",
      "[validation]accuracy/loss:0.9448214173316956/0.9448214173316956\n",
      "[validation]accuracy/loss:0.945263147354126/0.945263147354126\n",
      "[validation]accuracy/loss:0.9451724290847778/0.9451724290847778\n",
      "[validation]accuracy/loss:0.9450846910476685/0.9450846910476685\n",
      "[validation]accuracy/loss:0.9456666111946106/0.9456666111946106\n",
      "[validation]accuracy/loss:0.9460655450820923/0.9460655450820923\n",
      "[validation]accuracy/loss:0.9466128945350647/0.9466128945350647\n",
      "[validation]accuracy/loss:0.9457142353057861/0.9457142353057861\n",
      "[validation]accuracy/loss:0.9462500214576721/0.9462500214576721\n",
      "[validation]accuracy/loss:0.9460000395774841/0.9460000395774841\n",
      "[validation]accuracy/loss:0.946212112903595/0.946212112903595\n",
      "[validation]accuracy/loss:0.9461193680763245/0.9461193680763245\n",
      "[validation]accuracy/loss:0.9463235139846802/0.9463235139846802\n",
      "[validation]accuracy/loss:0.9463767409324646/0.9463767409324646\n",
      "[validation]accuracy/loss:0.9467142224311829/0.9467142224311829\n",
      "[validation]accuracy/loss:0.9467604756355286/0.9467604756355286\n",
      "[validation]accuracy/loss:0.9473612308502197/0.9473612308502197\n",
      "[validation]accuracy/loss:0.9475343227386475/0.9475343227386475\n",
      "[validation]accuracy/loss:0.9471622109413147/0.9471622109413147\n",
      "[validation]accuracy/loss:0.9474667310714722/0.9474667310714722\n",
      "[validation]accuracy/loss:0.9472368955612183/0.9472368955612183\n",
      "[validation]accuracy/loss:0.9470130801200867/0.9470130801200867\n",
      "[validation]accuracy/loss:0.9460257291793823/0.9460257291793823\n",
      "[validation]accuracy/loss:0.9463292360305786/0.9463292360305786\n",
      "[validation]accuracy/loss:0.9463750123977661/0.9463750123977661\n",
      "[validation]accuracy/loss:0.9462963342666626/0.9462963342666626\n",
      "[validation]accuracy/loss:0.9467073678970337/0.9467073678970337\n",
      "[validation]accuracy/loss:0.9472289681434631/0.9472289681434631\n",
      "[validation]accuracy/loss:0.9475000500679016/0.9475000500679016\n",
      "[validation]accuracy/loss:0.9476470947265625/0.9476470947265625\n",
      "[validation]accuracy/loss:0.9480233192443848/0.9480233192443848\n",
      "[validation]accuracy/loss:0.9482759237289429/0.9482759237289429\n",
      "[validation]accuracy/loss:0.9487499594688416/0.9487499594688416\n",
      "[validation]accuracy/loss:0.9485393166542053/0.9485393166542053\n",
      "[validation]accuracy/loss:0.9489999413490295/0.9489999413490295\n",
      "[validation]accuracy/loss:0.9494504928588867/0.9494504928588867\n",
      "[validation]accuracy/loss:0.9499999284744263/0.9499999284744263\n",
      "[validation]accuracy/loss:0.9502149820327759/0.9502149820327759\n",
      "[validation]accuracy/loss:0.9495744109153748/0.9495744109153748\n",
      "[validation]accuracy/loss:0.9493683576583862/0.9493683576583862\n",
      "[validation]accuracy/loss:0.949791669845581/0.949791669845581\n",
      "[validation]accuracy/loss:0.9498968720436096/0.9498968720436096\n",
      "[validation]accuracy/loss:0.9494898319244385/0.9494898319244385\n",
      "[validation]accuracy/loss:0.9500000476837158/0.9500000476837158\n",
      "[validation]accuracy/loss:0.9501000046730042/0.9501000046730042\n",
      "epoch:2,batch:0,loss is:[0.09380414]\n",
      "epoch:2,batch:200,loss is:[0.09145108]\n"
     ]
    }
   ],
   "source": [
    "model = LeNetModel()\n",
    "train(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "paddle.save(model.state_dict(), 'mnist-cnn.pdparams')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image  \n",
    "import numpy as np  \n",
    "import matplotlib.pyplot as plt  \n",
    " \n",
    "im = Image.open('0.jpg').convert('L')    \n",
    "im = im.resize((28, 28), Image.ANTIALIAS)  \n",
    "img = np.array(im).reshape(1, 1, 28, 28).astype('float32')   \n",
    "img = 1.0 - img / 255."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(2, 2))  \n",
    "plt.imshow(img[0][0], cmap=plt.cm.binary)  \n",
    "plt.show()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = LeNetModel()  \n",
    " \n",
    "params_file_path = 'mnist-cnn.pdparams'  \n",
    "param_dict = paddle.load(params_file_path)  \n",
    "model.load_dict(param_dict)  \n",
    " \n",
    "model.eval()  \n",
    "tensor_img = img  \n",
    "\n",
    "results = model(paddle.to_tensor(tensor_img))\n",
    "\n",
    "lab = np.argsort(results.numpy())  \n",
    "print(\"本次预测的数字是：\",lab[0][-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
